#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

#include "list.h"

using std::cout;
using std::endl;

List::List()
{
    _begin = 0;
    _end = 0;
}

List::~List()
{
    if( !isEmpty() )
    {
        Node* current = _begin;
        while( current != 0 )
        {
            _begin = current->_next;
            if( _begin != 0 )
                _begin->_prev = 0;

            delete current;
            current = _begin;
        }
    }
}

bool List::isEmpty()
{
    return _begin == 0;
}

void List::insertBack( int value )
{
    Node* newNode = new Node( value );
    if( isEmpty() )
    {
        _begin = _end = newNode;
    }
    else
    {
        _end->_next = newNode;
        newNode->_prev = _end;
        _end = newNode;
    }
}

void List::removeBack()
{
    if( isEmpty() )
        return;

    Node* current;
    current = _end;
    if( _begin == _end )
    {
        _begin = 0;
        _end = 0;
    }

    _end = _end->_prev;
    delete current;
    current = 0;
    _end->_next = 0;
}

void List::printList()
{
    Node* current;
    current = _begin;
    while( current != 0 )
    {
        cout << current->_value << ' ';
        current = current->_next;
    }
    cout << endl;
}


